const maps = {
	getLocation: (type = 'gcj02', geocode = true) => {
		return new Promise((res, err) => {
			uni.getLocation({
				type,
				geocode,
				success(e) {
					res(e)
				},
				fail(e) {
					err(e)
					console.log(e,'获取地址失败')
				}
			})
		})
	},
	transitionLongitude: (address) => { //根据地方名转换出经纬度
		return new Promise((resolve, reject) => {
			// let key = '7ed01ce750378a41fc02238addce3669'
			// let key = '17c2b90a5d9a08fdb32797aef50bc93a'
			let key = 'e51515724e4d7e46126c676226ebc416' //高德-陈刚
			uni.request({
				url: 'https://restapi.amap.com/v3/geocode/geo',
				data: {
					key,
					address
				},
				success: (res) => {
					let dst = {}
					let arr = res.data.geocodes[0].location.split(',')
					// #ifdef APP-PLUS
					dst = new plus.maps.Point(arr[0], arr[1]); // 天安门
					// #endif
					// #ifndef APP-PLUS
					dst = {
						latitude: Number(arr[1]),
						longitude: Number(arr[0]),
					}
					// #endif
					resolve(dst)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	},
	openMapApp: async (address, describe) => { //一键导航
		let dst = await maps.transitionLongitude(address)
		let position = {}
		plus.geolocation.getCurrentPosition(({
			coords
		}) => { //获取当前设备位置
			uni.hideLoading()
			position = {
				latitude: coords.latitude,
				longitude: coords.longitude,
			}
			let src = new plus.maps.Point(position.longitude, position.latitude); // 大钟寺
			plus.maps.openSysMap(dst, describe, src)
		}, err => {
			uni.hideLoading()
			uni.showToast({
				title: '导航失败'
			})
		})
	},
	openMap: async (address = '', dsts) => {
		let dst = {
			latitude: 39.92, //维度
			longitude: 116.46, //经度
		}
		if (dsts) {
			if (typeof dsts === 'object') {
				dst = dsts
			} else {
				try{
					dst = await maps.transitionLongitude(address)
				}catch(e){
					uni.showToast({
						title: e,
						icon:'none'
					})
				}
				
			}
		}

		uni.openLocation({
			...dst,
			name: address,
			scale: 15, //跳转地图页缩放比例
			address,
			fail: (err) => {
				console.log(err, 'err')
				uni.showToast({
					title: '导航失败'
				})
			}
		})
	},
	async chooseLocation(callback) {
		let [err, res] = await uni.chooseLocation();
		if(err) console.log(err,'调取位置接口失败')
		return [err, res]
	}
}
export default maps